export default {
	data() {
		return {};
	},
	config: {
		baseUrl: 'https://hh.sc.yifckj.com/',
		headers: {

		},
		header: {},
		dataType: 'json'
	},
	interceptor: {
		request: null,
		response: null
	},
	request(options) {
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			let _config = null;
			options.url = this.config.baseUrl + options.url;
			options.complete = (response) => {
				uni.hideLoading();
				let statusCode = response.statusCode;
				
				response.config = _config;

				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response);

					if (newResponse) {
						response = newResponse;
					}
				}
				if (statusCode === 200) {
					
					//成功
					if (response.data.code == 0) {
						if (response.data.msg == '请先登录') {
						  return uni.showModal({
								title: '系统提示',
								content: response.data.msg || '系统错误',
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										uni.reLaunch({
											url: "/pages/index/index"
										})
									}
								}
							});
							return false
						} else {
							console.log(123)
							return uni.showModal({
								title: '系统提示',
								content: response.data.msg || '系统错误',
								showCancel: false,
							});
						}

					} else if (response.data.code == 403) {
						uni.showModal({
							title: '提示',
							content: response.data.msg || '系统错误',
							showCancel: false
						});
						// uni.reLaunch({
						//     url: '/pages/Authorizedlogin/Authorizedlogin'
						// });
					} else {
						resolve(response.data);
					}
				} else {
					uni.showModal({
						title: '系统提示',
						content: response.data.msg || '系统错误',
						showCancel: false
					});
				}
			};

			_config = Object.assign({}, this.config, options);
			_config.requestId = new Date().getTime();

			if (this.interceptor.request) {
				this.interceptor.request(_config);
			} // Cache.getExpireCahe('UserAccessToken',true)
			// let token = Cache.get('UserAccessToken');
			// console.log('打印token',token)
			// if (token) {
			// 	_config.header['x-user-token'] = token;
			// } else {
			// 	login(_config, (oldConfig) => {
			// 		console.log('怎么回来了呢？需要打印回调',_config)
			// 		let token = Cache.get('UserAccessToken');
			// 		oldConfig.header['x-user-token'] = token;
			// 		uni.request(oldConfig);
			// 	})
			// 	return false
			// };

			uni.request(_config);
		});
	},
	get(url, data, options) {
		if (!options) {
			options = {};
		}

		options.url = url;
		options.data = data;
		options.method = 'GET';
		return this.request(options);
	},
	post(url, data, options) {
		if (!options) {
			options = {};
		}

		options.url = url;
		options.data = data;
		options.method = 'POST';
		return this.request(options);
	},
	put(url, data, options) {
		if (!options) {
			options = {};
		}

		options.url = url;
		options.data = data;
		options.method = 'PUT';
		return this.request(options);
	},
	delete(url, data, options) {
		if (!options) {
			options = {};
		}

		options.url = url;
		options.data = data;
		options.method = 'DELETE';
		return this.request(options);
	}
};
